package com.lagou.service.impl;

import com.lagou.dao.CodeDao;
import com.lagou.pojo.Code;
import com.lagou.service.CodeService;
import com.lagou.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.util.Date;
import java.util.Optional;
import java.util.Random;

/**
 * @author zy
 * @version 1.0
 * @date 2021/3/16 11:29
 */
@Service
public class CodeServiceImpl  implements CodeService {

    private final String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    @Autowired
    private CodeDao codeDao;

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public String toGenerateVerificationCode() {
        StringBuilder sb=new StringBuilder(6);
        for(int i=0;i<6;i++)
        {
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

    @Override
    public String doSendEmail(String code, String EmailAddress) {
        try {
            // 发送邮件
            String url = "http://lagou-service-email/api/email";
            MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
            paramMap.add("code",code);
            paramMap.add("emailAddress",EmailAddress);
            String status = restTemplate.postForObject(url, paramMap, String.class);

            // 对数据库进行查询，看该邮箱是否存在验证码，存在覆盖，不存在则保存
            // 对验证码和邮箱进行保存
            Code code1 = new Code();
            code1.setEmail(EmailAddress);
            Optional<Code> one = codeDao.findOne(Example.of(code1));
            if ( one.isPresent()){
                // 覆盖
                code1 = one.get();
                code1.setCode(code);
                code1.setCreatetime(DateUtil.getFormatNow("yyyy-MM-dd HH:mm:ss"));
                code1.setExpiretime(DateUtil.formatDatetime(DateUtil.addMinute(new Date(), 1)));
                codeDao.save(code1);
            }else {
                // 保存
                code1.setId((long) 0);
                code1.setCode(code);
                code1.setCreatetime(DateUtil.getFormatNow("yyyy-MM-dd HH:mm:ss"));
                code1.setExpiretime(DateUtil.formatDatetime(DateUtil.addMinute(new Date(), 1)));
                codeDao.save(code1);
            }

            return status;
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败";
        }
    }

    @Override
    public String inspectionCode(String code) throws ParseException {
        // 根据验证码查询数据库判断是否存在，并判断是否超过过期时间
        Code code1 = new Code();
        code1.setCode(code);
        Optional<Code> one = codeDao.findOne(Example.of(code1));
        if ( one.isPresent()){
            Code code2 = one.get();
            boolean after = DateUtil.isBefore(new Date(), DateUtil.parse(code2.getExpiretime(), "yyyy-MM-dd HH:mm:ss"));
            if (after){
                return "1";
            }else {
                return "2";
            }
        }else {
            return "0";
        }
    }

}
